package com.ccgk.logistics.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ccgk.common.MyCollectionUtils;
import com.ccgk.common.page.PaginationVo;
import com.ccgk.common.web.RequestUtils;
import com.ccgk.logistics.domain.Pack;
import com.ccgk.logistics.service.PackService;

@Controller
@RequestMapping("/pack")
public class PackController {
	
	@Autowired
	private PackService packService;
	
	/**查询列表*/
	@RequestMapping("/list")
	public String list(HttpServletRequest request){
		int currentPage = RequestUtils.getInt(request,"currentPage",1);
		int pageSize = RequestUtils.getInt(request,"pageSize",PaginationVo.DEFAULT_PAGESIZE);
		
		PaginationVo<Pack> result = packService.findList(currentPage, pageSize);
		request.setAttribute("result",result);
		return "pack/list";
	}
	
	/**获取单个*/
	@RequestMapping("/{id}")
	public String getById(@PathVariable("id")Integer id,Model model){
		Pack pack = packService.getById(id);
		model.addAttribute("obj", pack);
		return "pack/edit";
	}
	
	/**更新信息*/
	@ResponseBody
	@RequestMapping(value="/{id}",method=RequestMethod.POST)
	public String update(Pack pack){
		Pack _pack  = packService.getById(pack.getId());
		if(_pack != null && !_pack.getId().equals(pack.getId()))
			return "{flag:1}";
		packService.update(pack);
		return "{flag:0}";
	}
	/**新增页*/
	@RequestMapping("/add")
	public String showAdd(){
		return "pack/add";
	}
	/**新增*/
	@ResponseBody
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Pack pack,HttpServletResponse response){
		if(packService.getByName(pack.getName())!= null){
			return "{flag:1}";
		}
		packService.add(pack);
		return "{flag:0}";
	}
	/**删除*/
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestParam("id")String ids,Model model){
		Integer[] idIntArray = MyCollectionUtils.splitToIntArray(ids);
		packService.batchDelete(idIntArray);
		return "{success:true}";
	}
}
